home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1989 / 05 / rle.asc < prev    next >
Text File  |  1989-05-12  |  5KB  |  156 lines

  1. _RLE Revisited_
  2. by Phil Daley
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. /*****************************************************************************
  8. *      PROGRAM           RLE.C                             *
  9. *      written by Phil Daley                             *
  10. *      February 3, 1989                              *
  11. *****************************************************************************/
  12. int main(void);
  13. int uncompress(unsigned char *,int,unsigned char *) ;
  14. int compress(unsigned char *,int,unsigned char *) ;
  15. int process_comp(unsigned char *,int,int) ;
  16. int process_uncomp(unsigned char *,int,int) ;
  17. #include    <stdio.h>
  18. #include    <memory.h>
  19. unsigned char    screen[24][80] = {
  20. "╔══════════════════════════════════════════════════════════════════════════════╗",
  21. "║                                                                              ║",
  22. "║                                                                              ║",
  23. "║                                                                              ║",
  24. "║          o   This is a sample screen that would be typical of the type       ║",
  25. "║              that would present information to a user for instructions       ║",
  26. "║              or a help screen, etc.                                          ║",
  27. "║                                                                              ║",
  28. "║                                                                              ║",
  29. "║          o   While it contains a lot of unique characters in the text        ║",
  30. "║              lines, it also contains a lot of white space in empty lines     ║",
  31. "║              and margins.                                                    ║",
  32. "║                                                                              ║",
  33. "║                                                                              ║",
  34. "║          o   It would be unusual for the compression algorithm used          ║",
  35. "║              here to find any repeated characters other than spaces          ║",
  36. "║              and the border.                                                 ║",
  37. "║                                                                              ║",
  38. "║                                                                              ║",
  39. "║                                                                              ║",
  40. "║                                                                              ║",
  41. "║                                                                              ║",
  42. "║                                                                              ║",
  43. "╚══════════════════════════════════════════════════════════════════════════════╝"} ;
  44.  
  45. unsigned char    new[2000] ;
  46.  
  47. /**********************     main        ***********************/
  48. int main()          /*  this is a demo main  */
  49. {
  50.     int     orig_length = 1920 ;
  51.     int     compressed_length ;
  52.     int     i, j ;
  53.  
  54.     compressed_length = compress(screen[0],orig_length,new) ;
  55.     printf("The original screen (1920) compressed to %d bytes\n",compressed_length) ;
  56.     memset(screen,0,1920) ;        /*  erase the orig  */
  57.     orig_length = uncompress(new,compressed_length,screen[0]) ;
  58.     printf("And back to the original (%d) length\n",orig_length) ;
  59.     for (i = 0; i < 24; i++)
  60.     for (j = 0; j < 80; j++)    /*  show it  */
  61.         printf("%c",screen[i][j]) ;
  62.     return(0) ;
  63. }
  64. /***********************    compress    ****************************/
  65. compress(in_array,in_size,out_array)
  66. unsigned char    *in_array ;
  67. int        in_size ;
  68. unsigned char    *out_array ;
  69. {
  70.     register int i = 0 ;
  71.     register int j = 0 ;
  72.     register int k ;
  73.     register int l ;
  74.  
  75.     while (i < in_size) {
  76.     if (in_array[i] == in_array[i + 1]  && in_array[i + 1] == in_array[i + 2]) {
  77.         k = process_comp(in_array,i,in_size) ;
  78.         out_array[j++] = (unsigned char)k | 0x80 ;
  79.         out_array[j++] = in_array[i] ;
  80.         i += k ;
  81.     }
  82.     else  {
  83.         k = process_uncomp(in_array,i,in_size) ;
  84.         out_array[j++] = (unsigned char)k ;
  85.         for (l = 0; l < k; l++)
  86.         out_array[j++] = in_array[i++] ;
  87.     }
  88.     }
  89.     return(j) ;
  90. }
  91. /***********************    process_comp    ****************************/
  92. process_comp(in_array,i,in_size)
  93. unsigned char    *in_array ;
  94. int        i ;
  95. int        in_size ;
  96. {
  97.     register int len = 0 ;
  98.  
  99.     while (in_array[i] == in_array[i + 1] && i < in_size) {
  100.     len++ ;
  101.     i++ ;
  102.     if (len == 126)
  103.         break ;
  104.     }
  105.     return(len + 1) ;
  106. }
  107. /***********************    process_uncomp    ****************************/
  108. process_uncomp(in_array,i,in_size)
  109. unsigned char    *in_array ;
  110. int        i ;
  111. int        in_size ;
  112. {
  113.     register int len = 0 ;
  114.  
  115.     while ((in_array[i] != in_array[i + 1] || in_array[i] != in_array[i + 2]) && i < in_size) {
  116.     len++ ;
  117.     i++ ;
  118.     if (len == 127)
  119.         break ;
  120.     }
  121.     return(len) ;
  122. }
  123.  
  124.  
  125. /**********************     uncompress    ***********************/
  126. uncompress(in_array,in_size,out_array)
  127. unsigned char    *in_array ;
  128. int        in_size ;
  129. unsigned char    *out_array ;
  130. {
  131.     register int    i ;
  132.     register int    j ;
  133.     register int    k=0 ;
  134.     register int    l ;
  135.  
  136.     for (i = 0; i < in_size;) {
  137.     j = in_array[i++] ;
  138.     if (j > 128) {
  139.         for (j -= 128; j > 0; j--)
  140.         out_array[k++] = in_array[i] ;
  141.         i++ ;
  142.     }
  143.     else
  144.         for (l = 0; l < j; l++)
  145.         out_array[k++] = in_array[i++] ;
  146.     }
  147.     return(k) ;
  148. }
  149.  
  150.  
  151. -30-
  152.  
  153.  
  154.  
  155.  
  156.